# 存储数据
users = [
    {
        'id':1,
        'username':'admin',
        'password':'123'
    }
]

students = {
    1001:{
        'id':1001,
        'name':'aaa',
        'age':18,
        'sex':'男',
        'height':180.5,
        'major':'软件工程'
    },
    1002:{
        'id':1002,
        'name':'bbb',
        'age':19,
        'sex':'女',
        'height':150.2,
        'major':'物联网'
    }
}

sid = 1003 # 序列值，表示学号

# 用户登陆，如果成功返回True，否则返回False
def login(username,password):
    isSuccess = False
    for user in users:
        if user['username'] == username and user['password'] == password:
            isSuccess = True
            break
    return isSuccess

# 用户注册
def register(username,password):
    # 新增账户
    user = {
        'id': len(users) + 1,
        'username': username,
        'password': password
    }
    users.append(user)

# 查找所有学生
def findAll():
    print('学号', '姓名', '年龄', '性别', '身高', '专业', sep='\t\t')
    for stu in students.values():
        print(stu['id'], stu['name'], stu['age'], stu['sex'], stu['height'], stu['major'], sep='\t\t')

# 根据学号查找
def findById():
    id = int(input('请输入要查询的学号：'))
    if id in students:
        stu = students[id]
        print('学号', '姓名', '年龄', '性别', '身高', '专业', sep='\t\t')
        print(stu['id'], stu['name'], stu['age'], stu['sex'], stu['height'], stu['major'], sep='\t\t')
    else:
        print(f'未找到学号为{id}的学生')

# 添加学生
def add():
    name = input('请输入姓名：')
    for stu in students.values():
        if stu['name'] == name:
            name = name + '2'
            break
    age = int(input('请输入年龄：'))
    sex = input('请输入性别：')
    height = float(input('请输入身高：'))
    major = input('请输入专业：')
    global sid
    students[sid] = {
        'id': sid,
        'name': name,
        'age': age,
        'sex': sex,
        'height': height,
        'major': major
    }
    sid += 1
    print('添加成功！')

# 修改学生
def modify():
    id = int(input('请输入要修改的学生学号：'))
    if id in students:
        students[id]['name'] = input('请输入修改的姓名：')
        students[id]['age'] = int(input('请输入修改的年龄：'))
        students[id]['sex'] = input('请输入修改的性别：')
        students[id]['height'] = float(input('请输入修改的身高：'))
        students[id]['major'] = input('请输入修改的专业：')
        print('修改成功！')
    else:
        print(f'未找到学号为{id}的学生')

# 删除学生
def delete():
    id = int(input('请输入要删除的学生学号：'))
    if id in students:
        del students[id]
        print('删除成功！')
    else:
        print(f'未找到学号为{id}的学生')

print('=====================欢迎使用学生管理系统===========================')
while True:
    print('---------------------------------')
    print('1.登陆')
    print('2.注册')
    choice = int(input('请选择：'))
    if choice == 1:
        username = input('请输入用户名：')
        password = input('请输入密码：')
        # 判断是否登陆成功
        flag = login(username,password)
        if flag:
            print(f'欢迎您：{username}！')
            while True:
                print('---------------------------------')
                print('1.查询所有学生')
                print('2.根据学号查询学生')
                print('3.添加学生')
                print('4.修改学生')
                print('5.删除学生')
                print('6.退出')
                choice = int(input('请选择：'))
                if choice == 1:
                    findAll()
                elif choice == 2:
                    findById()
                elif choice == 3:
                    add()
                elif choice == 4:
                   modify()
                elif choice == 5:
                    delete()
                else:
                    break
        else:
            print('登陆失败，用户名或密码不正确！')
    elif choice == 2:
        username = input('请输入用户名：')
        password = input('请输入密码：')
        repassword = input('请输入确认密码：')
        # 1）判断用户名是否已存在
        isExist = False
        for user in users:
            if user['username'] == username:
                isExist = True
                break
        if isExist:
            print('用户名已存在，请重新注册！')
        elif password != repassword :
            # 2）判断两次输入的密码是否一致
            print('两次输入的密码不一致，请重新注册！')
        else:
            register(username,password)
            print('注册成功！', users)



























